<?php

namespace common\models\position;

use Yii;
use common\helpers\Url;
use common\helpers\ArrayHelper;
use common\components\ModelConstraint;
use common\models\member\Member;

/**
 * This is the model class for table "{{%position_content}}".
 *
 * @property int $id
 * @property int $position_id 推荐位ID
 * @property string $title 标题
 * @property string $description 描述
 * @property string $thumb 封面图
 * @property string $model 内部模型
 * @property string $model_id 内部模型ID
 * @property string $url 跳转地址
 * @property int $admin_id 管理员ID
 * @property int $map_id 内部路由ID
 * @property string $map_data 内部路由参数
 * @property int $type 内部链接类型
 * @property int $listorder 排序
 * @property int $status 状态：0禁用，10启用
 * @property int $created_at 创建时间
 * @property int $updated_at 修改时间
 */
class PositionData extends \common\db\ActiveRecord {

    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;

    public function resetType($type) {
        if (!$type) {
            return $type;
        }
    }

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%position_data}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['position_id', 'type', 'admin_id', 'map_id', 'listorder', 'status', 'created_at', 'updated_at'], 'integer'],
            [['title', 'thumb'], 'required'],
            [['title'], 'string', 'max' => 64],
            [['description', 'thumb', 'url', 'map_data'], 'string', 'max' => 255],
            [['model', 'model_id'], 'string', 'max' => 20],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'position_id' => '推荐位ID',
            'title' => '标题',
            'description' => '描述',
            'thumb' => '封面图',
            'url' => '跳转地址',
            'type' => '链接类型',
            'model' => '内部模型',
            'model_id' => '内部模型ID',
            'map_id' => '内部路由ID',
            'map_data' => '内部路由参数',
            'admin_id' => '管理员ID',
            'listorder' => '排序',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '修改时间',
        ];
    }

    public function getPosition() {
        return $this->hasOne(Position::className(), ['id' => 'position_id']);
    }

    public function getLink() {
        $type = is_numeric($this->type) ? intval($this->type) : null;
        $url = $this->url;
        $name = $this->model;
        if ($type == 302) {
            //外链优先级最高
            //加提示
            if (!$url || Url::isRelative($url)) {
                return null;
            }
        } elseif ($type == 0) {
            //其次是APP内部跳转
            $url = ModelConstraint::api($this->model, $this->model_id);
            if (!$url) {
                return null;
            }
        } else {
            //再次是H5内部跳转
            $url = ModelConstraint::link($this->model, $this->model_id, $type);
            if (!$url) {
                return null;
            }
            $type = 301;
            if ($this->model === "goods") {
                $type = 100;
            }
        }
        return compact('type', 'name', 'url');
    }

    public function getWeblink() {
        $data = [];
        if ($this->model_id) {
            $data['id'] = $this->model_id;
        }
        if (!empty($this->map_data)) {
            if (is_numeric($this->map_data)) {
                $temp = ['id' => $this->map_data];
            } else {
                $temp = unserialize($this->map_data);
            }
            if ($temp) {
                $data = ArrayHelper::merge($data, $temp);
            }
        }
        $mapid = $this->map_id;

        $memberMaps = [
            28 => 25
        ];
        if (Yii::$app->user->id && isset($memberMaps[$mapid])) {
            $member = Member::findAvailable(['user_id' => Yii::$app->user->id])->one();
            if ($member) {
                $mapid = $memberMaps[$mapid];
            }
        }

        return Url::weblink($mapid, $data);
    }

    public function fields() {
        return [
            'id',
            'title',
            'description',
            'thumb' => function() {
                return Url::attach($this->thumb);
            },
            'weblink',
        ];
    }

}
